package com.yuq.sunrise.util;

import com.yuq.sunrise.common.enums.ExceptionEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Alnwick
 */
public class ResultMap extends HashMap<String, Object>{

    private static final long serialVersionUID = 4347183762087136007L;

    public static final String R_KEY_STATUS = "status";
    public static final String R_KEY_CODE = "code";
    public static final String R_KEY_MSG = "msg";
    public static final String R_KEY_DATA = "data";

    public ResultMap() {
        put(R_KEY_CODE, ExceptionEnum.SUCCESS.getCode());
        put(R_KEY_MSG, ExceptionEnum.SUCCESS.getMsg());
    }

    public static ResultMap error() {
        return error(ExceptionEnum.UNKNOWN_ERROR.getCode(), ExceptionEnum.UNKNOWN_ERROR.getMsg());
    }

    public static ResultMap error(String msg) {
        return error(ExceptionEnum.UNKNOWN_ERROR.getCode(), msg);
    }

    public static ResultMap error(int code, String msg) {
        return new ResultMap().put(R_KEY_STATUS, code).put(R_KEY_MSG, msg);
    }

    public static ResultMap error(ExceptionEnum exceptionEnum) {
        return new ResultMap().put(R_KEY_STATUS, exceptionEnum.getCode()).put(R_KEY_MSG, exceptionEnum.getMsg());
    }


    public static ResultMap ok() {
        return new ResultMap();
    }

    public static ResultMap ok(String msg) {
        return new ResultMap().put(R_KEY_STATUS, ExceptionEnum.SUCCESS.getCode()).put(R_KEY_MSG,msg);
    }

    public static ResultMap ok(Object data) {
        return new ResultMap().put(R_KEY_STATUS, ExceptionEnum.SUCCESS.getCode()).put(R_KEY_MSG, ExceptionEnum.SUCCESS.getMsg()).
                put(R_KEY_DATA,data);
    }


    public static ResultMap ok(Map<String, Object> map) {
        ResultMap r = new ResultMap();
        r.putAll(map);
        return r;
    }

    @Override
    public ResultMap put(String key, Object value) {
        super.put(key, value);
        return this;
    }

}
